﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpfTypeConverterExample
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Human human = this.FindResource("human") as Human;
            MessageBox.Show(human.Child.Name);
        }
    }

    [TypeConverterAttribute(typeof(StringToHumanTypeConverter))]
    class Human
    {
        public string Name { get; set; }
        public Human Child { get; set; }
    }

    public class StringToHumanTypeConverter : TypeConverter
    {
        public override object ConvertFrom(ITypeDescriptorContext context, 
            System.Globalization.CultureInfo culture, 
            object value)
        {
            if (value is string)
	        {
                Human human = new Human();
                human.Name = value as string;
                return human;
	        }
            return base.ConvertFrom(context, culture, value);
        }
    }
}
